#!/bin/sh

# 这是一个Win10 for MSYS2 install shell 脚本，通过该脚本可以检查并安装 MinGW 工具链
# 它将安装 MinGW 工具链并检查 mingw-w64-ucrt-x86_64-make、mingw-w64-ucrt-x86_64-cmake、mingw-w64-ucrt-x86_64-poco 和 mingw-w64-ucrt-x86_64-fltk 是否安装
# 并安装 mingw-w64-ucrt-x86_64-fltk 的可选库 cairo, OpenGL, doxygen, zlib

echo "欢迎使用 Win10 for MSYS2 install shell 脚本！"
echo "本脚本将检查并安装以下工具链："
echo " 1. mingw-w64-ucrt-x86_64-make"
echo " 2. mingw-w64-ucrt-x86_64-cmake"
echo " 3. mingw-w64-ucrt-x86_64-toolchain"
echo " 4. mingw-w64-ucrt-x86_64-poco"
echo " 5. mingw-w64-ucrt-x86_64-fltk"
echo " 6. curl"
echo "并安装mingw-w64-ucrt-x86_64-fltk 的可选库："
echo "  1. cairo"
echo "  2. OpenGL"
echo "  3. doxygen"
echo "  4. zlib"
echo "本脚本仅支持 MSYS2在 64 位 Windows 上运行环境。"
echo "请按任意键继续..."
read -r
echo "正在检查和安装必要的软件包..."

# 检查并安装 curl
check_and_install_curl() {
    package=mingw-w64-ucrt-x86_64-curl
    command_name=curl
    if command -v "$command_name" &> /dev/null
    then
        echo "$package 已成功安装。"
    else
        echo "$package 未安装。正在更新包数据库并安装..."
        pacman -Syu --noconfirm
        pacman -S --noconfirm "$package"
        if command -v "$command_name" &> /dev/null
        then
            echo "$package 安装成功。"
        else
            echo "安装 $package 失败。请手动安装。"
            exit 1
        fi
    fi
}

# 检查并安装 MinGW 工具链
check_and_install_toolchain() {
    package=mingw-w64-ucrt-x86_64-toolchain
    if pacman -Qs "$package" &> /dev/null
    then
        echo "$package 已成功安装。"
    else
        echo "$package 未安装。正在更新包数据库并安装..."
        pacman -Syu --noconfirm --ignore=mingw-w64-ucrt-x86_64-fltk
        pacman -S --noconfirm "$package"
        if pacman -Qs "$package" &> /dev/null
        then
            echo "$package 安装成功。"
        else
            echo "target not found: $package"
        fi
    fi
}

# 检查并安装 mingw-w64-ucrt-x86_64-make
check_and_install_make() {
    package=mingw-w64-ucrt-x86_64-make
    command_name=mingw32-make
    if command -v "$command_name" &> /dev/null
    then
        echo "$package 已成功安装。"
    else
        echo "$package 未安装。正在更新包数据库并安装..."
        pacman -Syu --noconfirm --ignore=mingw-w64-ucrt-x86_64-fltk
        pacman -S --noconfirm "$package"
        if command -v "$command_name" &> /dev/null
        then
            echo "$package 安装成功。"
        else
            echo "target not found: $package"
        fi
    fi
}

# 检查并安装 mingw-w64-ucrt-x86_64-cmake
check_and_install_cmake() {
    package=mingw-w64-ucrt-x86_64-cmake
    command_name=cmake
    if command -v "$command_name" &> /dev/null
    then
        echo "$package 已成功安装。"
    else
        echo "$package 未安装。正在更新包数据库并安装..."
        pacman -Syu --noconfirm --ignore=mingw-w64-ucrt-x86_64-fltk
        pacman -S --noconfirm "$package"
        if command -v "$command_name" &> /dev/null
        then
            echo "$package 安装成功。"
        else
            echo "target not found: $package"
        fi
    fi
}

# 检查并安装 mingw-w64-ucrt-x86_64-poco
check_and_install_poco() {
    package=mingw-w64-ucrt-x86_64-poco
    if pacman -Qs "$package" &> /dev/null
    then
        echo "$package 已成功安装。"
    else
        echo "$package 未安装。正在更新包数据库并安装..."
        pacman -Syu --noconfirm --ignore=mingw-w64-ucrt-x86_64-fltk
        pacman -S --noconfirm "$package"
        if pacman -Qs "$package" &> /dev/null
        then
            echo "$package 安装成功。"
        else
            echo "target not found: $package"
        fi
    fi
}

# 检查并安装 mingw-w64-ucrt-x86_64-fltk 版本 1.3.x
check_and_install_fltk() {
    local package=mingw-w64-ucrt-x86_64-fltk
    local fltk_version
    local latest_fltk_version

    # 获取最新的 fltk 1.3.x 版本
    latest_fltk_version=$(get_latest_fltk_version)

    if pacman -Qs "$package" &> /dev/null; then
        fltk_version=$(pacman -Q "$package" | awk '{print $2}')
        if [[ $fltk_version == 1.3.* ]]; then
            echo "$package 版本 $fltk_version 已安装。"
            # 检查是否需要更新
            if [[ $fltk_version != $latest_fltk_version ]]; then
                echo "当前版本 $fltk_version 小于最新版本 $latest_fltk_version。是否更新到最新版本？(y/n)"
                read -r answer
                if [[ $answer == "y" ]]; then
                    pacman -Syu --noconfirm --ignore=mingw-w64-ucrt-x86_64-fltk
                    pacman -S --noconfirm "$package=$latest_fltk_version"
                    if pacman -Qs "$package" &> /dev/null; then
                        fltk_version=$(pacman -Q "$package" | awk '{print $2}')
                        echo "$package 版本 $fltk_version 更新成功。"
                    else
                        echo "安装 $package=$latest_fltk_version 失败。尝试从网络下载..."
                        download_and_install_package "$package" "$latest_fltk_version"
                    fi
                else
                    echo "跳过更新。"
                fi
            else
                echo "$package 已是最新版本。"
            fi
        else
            echo "$package 版本 $fltk_version 已安装，但不是 1.3.x 版本。是否卸载并重新安装 1.3.x 版本？(y/n)"
            read -r answer
            if [[ $answer == "y" ]]; then
                pacman -Rns --noconfirm "$package"
                echo "$package 卸载完成。正在更新包数据库并安装 1.3.x 版本..."
                pacman -Syu --noconfirm --ignore=mingw-w64-ucrt-x86_64-fltk
                pacman -S --noconfirm "$package=$latest_fltk_version"
                if pacman -Qs "$package" &> /dev/null; then
                    fltk_version=$(pacman -Q "$package" | awk '{print $2}')
                    if [[ $fltk_version == 1.3.* ]]; then
                        echo "$package 版本 $fltk_version 安装成功。"
                    else
                        echo "安装 $package=$latest_fltk_version 失败。尝试从网络下载..."
                        download_and_install_package "$package" "$latest_fltk_version"
                    fi
                else
                    echo "安装 $package=$latest_fltk_version 失败。尝试从网络下载..."
                    download_and_install_package "$package" "$latest_fltk_version"
                fi
            else
                echo "跳过卸载和重新安装。"
            fi
        fi
    else
        echo "$package 未安装。正在更新包数据库并安装 1.3.x 版本..."
        pacman -Syu --noconfirm --ignore=mingw-w64-ucrt-x86_64-fltk
        pacman -S --noconfirm "$package=$latest_fltk_version"
        if pacman -Qs "$package" &> /dev/null; then
            fltk_version=$(pacman -Q "$package" | awk '{print $2}')
            if [[ $fltk_version == 1.3.* ]]; then
                echo "$package 版本 $fltk_version 安装成功。"
            else
                echo "安装 $package=$latest_fltk_version 失败。尝试从网络下载..."
                download_and_install_package "$package" "$latest_fltk_version"
            fi
        else
            echo "安装 $package=$latest_fltk_version 失败。尝试从网络下载..."
            download_and_install_package "$package" "$latest_fltk_version"
        fi
    fi
}
#通过curl 获取FLTK1.3.x版本号
get_latest_fltk_version() {
    local latest_version
    #latest_version=$(curl -s https://www.fltk.org/pub/fltk/1.3/ | grep -oP 'fltk-\K1\.3\.[0-9]+' | sort -V | tail -n 1)
    latest_version=1.3.9-2
    echo "$latest_version"
}

# 检查并安装 mingw-w64-ucrt-x86_64-fltk 的可选库
install_optional_packages() {
    packages=("mingw-w64-ucrt-x86_64-cairo" "mingw-w64-ucrt-x86_64-opengl-man-pages" "mingw-w64-ucrt-x86_64-doxygen" "mingw-w64-ucrt-x86_64-zlib")
    for package in "${packages[@]}"; do
        if pacman -Qs "$package" &> /dev/null
        then
            echo "$package 已成功安装。"
        else
            echo "$package 未安装。正在更新包数据库并安装..."
            pacman -Syu --noconfirm --ignore=mingw-w64-ucrt-x86_64-fltk
            pacman -S --noconfirm "$package"
            if pacman -Qs "$package" &> /dev/null
            then
                echo "$package 安装成功。"
            else
                echo "target not found: $package"
            fi
        fi
    done
}

# 检查并安装 mingw-w64-ucrt-x86_64-libjpeg-turbo
check_and_install_libjpeg_turbo() {
    package=mingw-w64-ucrt-x86_64-libjpeg-turbo
    if pacman -Qs "$package" &> /dev/null
    then
        echo "$package 已成功安装。"
    else
        echo "$package 未安装。正在更新包数据库并安装..."
        pacman -Syu --noconfirm --ignore=mingw-w64-ucrt-x86_64-fltk
        pacman -S --noconfirm "$package"
        if pacman -Qs "$package" &> /dev/null
        then
            echo "$package 安装成功。"
        else
            echo "target not found: $package"
        fi
    fi
}

# 检查并安装 mingw-w64-ucrt-x86_64-libpng
check_and_install_libpng() {
    package=mingw-w64-ucrt-x86_64-libpng
    if pacman -Qs "$package" &> /dev/null
    then
        echo "$package 已成功安装。"
    else
        echo "$package 未安装。正在更新包数据库并安装..."
        pacman -Syu --noconfirm --ignore=mingw-w64-ucrt-x86_64-fltk
        pacman -S --noconfirm "$package"
        if pacman -Qs "$package" &> /dev/null
        then
            echo "$package 安装成功。"
        else
            echo "target not found: $package"
        fi
    fi
}

# 检查并安装 pkg-config
check_and_install_pkg_config() {
    package=mingw-w64-ucrt-x86_64-pkg-config
    if pacman -Qs "$package" &> /dev/null
    then
        echo "$package 已成功安装。"
    else
        echo "$package 未安装。正在更新包数据库并安装..."
        pacman -Syu --noconfirm --ignore=mingw-w64-ucrt-x86_64-fltk
        pacman -S --noconfirm "$package"
        if pacman -Qs "$package" &> /dev/null
        then
            echo "$package 安装成功。"
        else
            echo "target not found: $package"
        fi
    fi
}

# 从镜像站下载并安装指定的包
download_and_install_package() {
    local package=$1
    local version=$2
    local url="https://repo.msys2.org/mingw/ucrt64/$package-$version-any.pkg.tar.zst"
    
    if [ -z "$version" ]; then
        url="https://repo.msys2.org/mingw/ucrt64/$package-any.pkg.tar.zst" 
    fi
    echo "包名 $package"
    echo "版本 $version"
    echo "尝试从 $url 下载 $package..."
    mkdir -p ./downloads/$package
    curl -L -o /tmp/$package.pkg.tar.zst "$url"
    if [ $? -eq 0 ]; then
        echo "下载成功，正在安装..."
        pacman -U --noconfirm /tmp/$package.pkg.tar.zst
        if [ $? -eq 0 ]; then
            echo "$package 安装成功。"
        else
            echo "手动安装 $package 失败。请手动处理。"
        fi
    else
        echo "下载 $package 失败。请手动处理。"
    fi
}


# 启动时检查 curl 是否安装
check_and_install_curl

# 执行安装和检查
check_and_install_toolchain
check_and_install_make
check_and_install_cmake
check_and_install_poco
check_and_install_fltk
install_optional_packages
check_and_install_libjpeg_turbo
check_and_install_libpng

# 检查并安装 pkg-config
check_and_install_pkg_config

echo "当前日期和时间是："
date
echo "当前目录下的文件和文件夹有："
ls -l